#!/usr/bin/perl
# Este script lee el fichero index.txt y abre uno a uno los ficheros referenciados en el.
# Cada una de los caracteres de estos ficheros referenciados son resueltos en codigos HID
# que se imprimiran en un array al final de la ejecucion.
# Cuando se acaba de codificar un fichero y se pasa al siguiente, dentro del array se 
# incluira el codigo 0, para que el sofware pueda interpretar que se ha cambiado y ejecutar
# la accion apropiada (esperar hasta que el usuario pulse el boton, esperar X milisegundos,
# un cambio en los leds de CAPSLOCK, NUMLOCK, BLOQDESPL)
#
# Esta version no contempla caracteres fuera de los encontrados en el array @mins y @mays

# CHARS
@mins=('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','1','2','3','4','5','6','7','8','9','0','-','=','[',']','\\',';',"\'",'`',',','.','/',' ','\n','\t');
@mays=('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','!','@','#','$','%','^','&','*','(',')','_','+','{','}','|',':','"','~','<','>','?',' ','\n','\t');
@minsmayscode=(4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,45,46,47,48,49,51,52,53,54,55,56,44,40,43);

# EXTRAS
@extra=("Print Screen","Scroll Lock","Break (Ctrl-Pause)","Pause","Insert","Home","Page Up","Delete","End","Page Down","Right Arrow","Left Arrow","Down Arrow","Up Arrow","Num Lock","Keypad /","Keypad *","Keypad -","Keypad +","Keypad Enter","Keypad 1 End","Keypad 2 Down","Keypad 3 PageDn","Keypad 4 Left","Keypad 5","Keypad 6 Right","Keypad 7 Home","Keypad 8 Up","Keypad 9 PageUp","Keypad 0 Insert","Keypad . Delete","Europe 2","App","Keyboard Power","Keypad =","F13","F14","F15","F16","F17","F18","F19","F20","F21","F22","F23","F24","Keyboard Execute","Keyboard Help","Keyboard Menu","Keyboard Select","Keyboard Stop","Keyboard Again","Keyboard Undo","Keyboard Cut","Keyboard Copy","Keyboard Paste","Keyboard Find","Keyboard Mute","Keyboard Volume Up","Keyboard Volume Dn","Caps Lock","Num Lock","Scroll Lock","Keyboard Equal Sign");
@extra_code=(70,71,72,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,134);

$index_file="index.txt";
open(INDEX, $index_file) || die('Could not open file!'); 

my $found=0;
my $count=0;
my $charcount=0;
my $keys = "unsigned char keys[] = {\\\n";
my $keys_caps = "unsigned int keys_caps[] = {";
while (my $file = <INDEX>){
	chomp($file);
	open(INPUT, $file) || die("Could not open file: " . $file . "\n");
	while(my $line = <INPUT>){
		# Si la linea contiene un tag de control
#		if ( $line =~ /^</ ){
#		}
		while(length($line)>0){
			if ($count>10){
				$keys .= "\\\n";
				$count=0;
			}

			if($found == 1){
				$found=0;
				$charcount++;
			}

			for ($ci=0; $ci<@mins; $ci++){
				if ( index($line, $mins[$ci]) == 0 ){
					$line=substr($line, length($mins[$ci]), length($line));
					$keys .= $minsmayscode[$ci] . ", ";
					$count++;
					$ci=0;
					$found=1;
					last;	
				}
			}
			if($found==1){
				next;
			}
			for ($ci=0; $ci<@mays; $ci++){
				if ( index($line, $mays[$ci]) == 0 ){
					$line=substr($line, length($mays[$ci]), length($line));
					$keys .= $minsmayscode[$ci] . ", ";
					$keys_caps .= $charcount . ", ";
					$count++;
					$ci=0;
					$found=1;
					last;	
				}
			}	
			if($found==1){
				next;
			}
			$line=substr($line, 1, length($line));
		}
		$keys .= "40, ";
		$charcount++;
		$count++;
	}
	$keys .= "0, ";
	$charcount++;
	$count++;
}

$keys .= "};\n";
$keys_caps .= "};\n";
close(INDEX);

print $keys;
print $keys_caps;
